﻿using BLevy.Negocio;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BLevy.UI
{
    public partial class frmCobrosConsulta : Form
    {
        Utils.Numeros n = new Utils.Numeros();

        public frmCobrosConsulta()
        {
            InitializeComponent();
        }

        private void IniciarControles()
        {
            txtNroCupon.Text = string.Empty;

            cboFormaPago.Enabled = false;
            dtpDesde.Enabled = false;
            dtpHasta.Enabled = false;

            txtNroCupon.Focus();
        }

        private void frmCobrosConsulta_Load(object sender, EventArgs e)
        {
            IniciarControles();
            FindFormasPago();

            FiltrarForm();
        }

        private void FindFormasPago()
        {
            try
            {
                cboFormaPago.DisplayMember = "forma_pago";
                cboFormaPago.ValueMember = "id";
                cboFormaPago.DataSource = FormasPagos.FindAll();

                cboFormaPago.SelectedIndex = -1;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error : " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FiltrarForm()
        {

            string numero_cupon = txtNroCupon.Text == string.Empty ? null : txtNroCupon.Text;
            int? formapago_id = ckbFormaPago.Checked ? (int?)cboFormaPago.SelectedValue : null;
            DateTime? desde = ckbFechas.Checked ? (DateTime?)dtpDesde.Value : null;
            DateTime? hasta = ckbFechas.Checked ? (DateTime?)dtpHasta.Value : null;

            try
            {
                var query = (from row in Cobros.FindAllFiltro(numero_cupon, formapago_id, desde, hasta)
                             select new
                             {
                                 row.fecha,
                                 row.cupon.numero,
                                 row.cupon.fecha_emision,
                                 row.formapago.forma_pago,
                                 row.total

                             }).ToList();

                dgvCobros.Rows.Clear();
                foreach (var row in query)
                {
                    dgvCobros.Rows.Add(row.fecha.ToShortDateString(), row.numero, row.fecha_emision.ToShortDateString(), row.forma_pago, n.AgregarDecimales(row.total.ToString()));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error : " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ckbCobrador_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbFormaPago.CheckState == CheckState.Checked)
            {
                cboFormaPago.Enabled = true;
                cboFormaPago.Focus();
            }
            else
            {
                cboFormaPago.Enabled = false;
                cboFormaPago.SelectedIndex = -1;
            }
        }

        private void ckbFechas_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbFechas.CheckState == CheckState.Checked)
            {
                dtpDesde.Enabled = true;
                dtpHasta.Enabled = true;

                dtpDesde.Focus();
            }
            else
            {
                dtpDesde.Enabled = false;
                dtpHasta.Enabled = false;

                dtpDesde.Value = DateTime.Now;
                dtpHasta.Value = DateTime.Now;
            }
        }

        private void txtNroCupon_TextChanged(object sender, EventArgs e)
        {
            FiltrarForm();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            FiltrarForm();
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
